#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <config.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <weather.h>
#include <iconv.h>

static pthread_t weatherTid;
static int clientfd;
static int selectFlag;

//字符编码转换
static int code_convert(char *from_charset,char *to_charset,char *inbuf,size_t inlen,char *outbuf,size_t outlen)
{
	iconv_t cd;
	char **pin = &inbuf;
	char **pout = &outbuf;

	cd = iconv_open(to_charset,from_charset);
	if (cd==0) 
		return -1;
	
	memset(outbuf,0,outlen);
	if (iconv(cd,pin,&inlen,pout,&outlen)==-1) 
	{
		fprintf(stderr,"iconv error:%s\n",strerror(errno));
		return -2;
	}
	
	iconv_close(cd);
	return 0;
}

static int analyzeData(int lenth, char *buff)
{
	int selectProvinces;
	int selectCity;
	static int cnt=0;
	static char *(*city)[2];
	char cityBuff[32];
	char *tips="请选择城市:";
	PT_provinceST me = provinces;
	int provincesNum = 34;
	char Buff[32];
	size_t lenth1,lenth2=32;

	if(selectFlag ==0)
	{
		selectProvinces = atoi(buff);
		if(selectProvinces<0 || selectProvinces> provincesNum)
		{
			fprintf(stderr,"provinces select error\n");
			return 1;
		}
	
		city = me[selectProvinces].provinceAddr;
		cnt  = 0;
		lenth1 = strlen(tips);
		memset(Buff,0,32);
		code_convert("utf-8","gb2312",tips,lenth1,Buff,lenth2);
		send(clientfd,Buff,strlen(Buff),0);	
		
		while(city[cnt][0] != NULL)
		{
			memset(cityBuff,0,32);
			snprintf(cityBuff,31,"%02d %s\n",cnt,city[cnt][0]);
			lenth1 = strlen(cityBuff);
			memset(Buff,0,32);
			code_convert("utf-8","gb2312",cityBuff,lenth1,Buff,lenth2);
			send(clientfd,Buff,strlen(Buff),0);
			cnt++;
		}
		selectFlag =1;
	}
	else if(selectFlag ==1)
	{
		selectFlag=0;
		selectCity = atoi(buff);
		if(selectCity<0 || selectCity>= cnt)
		{
			fprintf(stderr,"city select error\n");
			return 1;
		}
		setCurrentCity(city[selectCity][1]);
		timeCnt =0;
		return 1;
	}

	
	return 0;
}

static void dataAccept(void)
{
	int ret;
	int lenth;
	char recvBuff[32]={0};

	while(1)
	{
		memset(recvBuff,0,32);
		lenth = recv(clientfd,recvBuff,32,0);
		if(lenth <0)
		{
			if(errno == EAGAIN || errno == EINTR)
				continue;
			close(clientfd);
			break;
		}
		if(lenth ==0)
		{
			close(clientfd);
			break;
		}
		else
		{
			ret = analyzeData(lenth, recvBuff);
			if(ret>0)
			{
				close(clientfd);
				break;
			}
		}
	}
}

static void *weatherSetHandler(void *ptr)
{
	char ip[20];
	int ret;
	struct sockaddr_in raddr;
	socklen_t rlenth = sizeof(raddr);
	PT_provinceST me = provinces;
	int provincesNum = 34;
	char provincesBuff[32];
	char *tips="请选择省/市:";
	char Buff[32];
	size_t lenth1,lenth2=32;

	ret = g_ptNetServer->networkInit();
	if(ret <0)
	{
		fprintf(stderr,"networkInit error\n");
		return NULL;
	}
	
	while(1)
	{
		clientfd = accept(g_ptNetServer->fd, (void *)&raddr, &rlenth);
		if(clientfd <0)
		{
			if(errno == EAGAIN || errno == EINTR)
				continue;
			fprintf(stderr,"accept error %s\n",strerror(errno));
		}
		
		memset(ip,0,sizeof(ip));
		inet_ntop(AF_INET,&raddr.sin_addr.s_addr,ip,sizeof(ip));
		fprintf(stderr,"clientIP:%s clientPORT:%d\n",ip,ntohs(raddr.sin_port));

		selectFlag =0;
		lenth1 = strlen(tips);
		memset(Buff,0,32);
		code_convert("utf-8","gb2312",tips,lenth1,Buff,lenth2);
		send(clientfd,Buff,strlen(Buff),0);
		
		for(int i=0;i<provincesNum;i++)
		{
			memset(provincesBuff,0,32);
			snprintf(provincesBuff,31,"%02d %s\n",i,me[i].provinceName);
			lenth1 = strlen(provincesBuff);
			memset(Buff,0,32);
			code_convert("utf-8","gb2312",provincesBuff,lenth1,Buff,lenth2);
			send(clientfd,Buff,strlen(Buff),0);
		}
		
		dataAccept();
	}

	pthread_exit(NULL);
}

int WeatherServerInit(void)
{
	int ret;
	 
	ret = pthread_create(&weatherTid,NULL,weatherSetHandler,NULL);
	if(ret !=0)
	{
		fprintf(stderr,"pthread_create weatherSetHandler error %s\n",strerror(errno));
		return -1;
	}

	return 0;
}

int WeatherServerExit(void)
{
	g_ptNetServer->networkExit();
	pthread_cancel(weatherTid);
	pthread_join(weatherTid,NULL);

	return 0;
}

